# -*- coding:utf-8 -*-

from pydantic import BaseModel, ConfigDict
from typing import Optional
from datetime import datetime


class SearchTaskModel(BaseModel):
    """搜索任务数据模型"""
    model_config = ConfigDict(from_attributes=True)
    
    id: Optional[int] = None
    task_name: Optional[str] = None
    keyword: Optional[str] = None
    total_time: Optional[int] = None
    interval_time: Optional[int] = None
    task_status: Optional[str] = None
    search_count: Optional[int] = None
    last_search_time: Optional[datetime] = None
    auto_order: Optional[str] = None
    remark: Optional[str] = None
    status: Optional[str] = None
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None


class SearchTaskQueryModel(BaseModel):
    """搜索任务查询参数模型"""
    task_name: Optional[str] = None
    keyword: Optional[str] = None
    task_status: Optional[str] = None
    status: Optional[str] = None


class SearchTaskCreateModel(BaseModel):
    """搜索任务创建模型"""
    task_name: str
    keyword: str
    total_time: int = 60
    auto_order: Optional[str] = '1'
    remark: Optional[str] = None


class SearchTaskUpdateModel(BaseModel):
    """搜索任务更新模型"""
    task_name: Optional[str] = None
    keyword: Optional[str] = None
    total_time: Optional[int] = None
    auto_order: Optional[str] = None
    remark: Optional[str] = None
    status: Optional[str] = None


class OrderRecordModel(BaseModel):
    """下单记录数据模型"""
    model_config = ConfigDict(from_attributes=True)
    
    id: Optional[int] = None
    task_id: Optional[int] = None
    item_id: Optional[str] = None
    item_title: Optional[str] = None
    item_price: Optional[str] = None
    pic_url: Optional[str] = None  # ✅ 商品图片URL
    client_id: Optional[str] = None
    client_name: Optional[str] = None
    client_remark: Optional[str] = None  # ✅ 客户端备注
    order_status: Optional[str] = None
    order_result: Optional[str] = None
    error_msg: Optional[str] = None
    is_auto_order: Optional[bool] = None  # ✅ 是否自动下单
    order_time: Optional[datetime] = None
    remark: Optional[str] = None
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None
    
    # ✅ 订单信息字段（缺失的字段）
    biz_order_id: Optional[str] = None
    biz_order_id_str: Optional[str] = None
    pay_order_id: Optional[str] = None
    pay_url: Optional[str] = None
    service_order: Optional[str] = None
    use_pay_url: Optional[str] = None
    account_user_id: Optional[str] = None
    user_name: Optional[str] = None
    keyword: Optional[str] = None
    seller_id: Optional[str] = None
    seller_name: Optional[str] = None
    
    # ✅ BaseMixin字段（修复：添加缺失的字段）
    create_by: Optional[int] = None
    dept_id: Optional[int] = None


class OrderRecordQueryModel(BaseModel):
    """下单记录查询参数模型"""
    task_id: Optional[int] = None
    item_id: Optional[str] = None
    item_title: Optional[str] = None
    client_id: Optional[str] = None
    keyword: Optional[str] = None
    seller_id: Optional[str] = None
    order_status: Optional[str] = None
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None


class ManualSearchRequestModel(BaseModel):
    """手动搜索请求模型"""
    keyword: str
    min_price: Optional[float] = 0.0
    max_price: Optional[float] = 0.0
    order_price: Optional[float] = 0.0
    total_time: Optional[int] = 60
    enable_auto_order: Optional[bool] = False
    display_count: Optional[int] = 30


class SearchConfigModel(BaseModel):
    """搜索配置模型（用于保存/加载配置）"""
    keyword: Optional[str] = None
    min_price: Optional[float] = 0.0
    max_price: Optional[float] = 0.0
    order_price: Optional[float] = 0.0
    total_time: Optional[int] = 60
    enable_auto_order: Optional[bool] = False
    skip_first_search: Optional[bool] = False  # ✅ 首次搜索不自动下单
    enable_jitter: Optional[bool] = True  # ✅ 抖动搜索（默认开启）
    display_count: Optional[int] = 30
    seller_blacklist: Optional[list] = None
    title_blacklist: Optional[list] = None

